<!--
 * @Descripttion:
 * @Author: voanit
 * @Date: 2021-10-12 16:10:18
 * @LastEditors: voanit
 * @LastEditTime: 2021-10-18 10:40:43
-->
<template>
  <el-card class="userAvatar-container">
    <!-- 面板屑导航 -->
    <el-breadcrumb separator="/" style="margin-bottom: 40px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>更换头像</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 用来展示头像的盒子 -->
    <div class="avatar-box">
      <img :src="userImg || ImgDefault" alt="" />
    </div>
    <!-- 按钮 -->
    <div class="btns">
      <!-- 文件选择框 -->
      <input
        type="file"
        accept="image/*"
        style="display: none"
        ref="fileRef"
        @change="onFileChange"
      />
      <el-button type="primary" @click="chooseImage" icon="el-icon-plus"
        >选择图片</el-button
      >
      <el-button
        type="success"
        icon="el-icon-upload"
        :disabled="isDisabled"
        @click="uploadAvatar"
        >上传头像
      </el-button>
    </div>
  </el-card>
</template>

<script>
import { reqUpdateAvatar } from '@/api/user'
import ImgDefault from '@/assets/images/avatar.jpg'
import { mapState, mapActions } from 'vuex'
export default {
  name: '',
  data () {
    return {
      // 头像的图片
      // imgURL: require('@/assets/images/avatar.jpg')
      // imgURL: '@/assets/images/avatar.jpg'
      ImgDefault,
      userImg: '',
      // 上传按钮是否被禁用
      isDisabled: true
    }
  },
  async created () {
    await this.getUserInfo()
    this.userImg = this.userInfo.user_pic
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  methods: {
    ...mapActions('user', ['getUserInfo']),
    chooseImage () {
      this.$refs.fileRef.click()
    },
    // 文件选择框发生了 change 变化
    onFileChange (e) {
      console.log(e, 333)
      const file = e.target.files[0]
      // 1. 判断是否选择了图像
      if (!file) {
        this.isDisabled = true
        this.userImg = ImgDefault
        return this.$message.warning('请选择上传的图像')
      }
      // 2. 限制上传文件的大小 1.5M
      if (file.size / 1024 / 1024 > 1.5) {
        return this.$message.warning('图像大小不能超过1.5M')
      }
      // 3. 处理base64格式图片 回显
      //  3.1 使用fileReader文件读取对象 处理base64格式
      const fileReader = new FileReader()
      // 3.2 使用fileReader实例读取文件地址readAsDataURL()
      fileReader.readAsDataURL(file)
      // 3.3在上传成功后load事件中,获取文件的base64格式的地址
      fileReader.addEventListener('load', () => {
        console.log(fileReader.result) // base64格式的图片
        this.userImg = fileReader.result
        console.log(fileReader.result, 3)
        // 3.4 启用禁用上传的按钮
        this.isDisabled = false
      })
    },
    async uploadAvatar () {
      const { data } = await reqUpdateAvatar(this.userImg)
      // 2 提示
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      // 3. 重新获取个人信息
      this.getUserInfo()
      // 4. 禁用按钮
      this.isDisabled = true
    }
  }
}
</script>
<style lang='less'  scoped>
.avatar-box {
  img {
    width: 350px;
    height: 350px;
    object-fit: cover;
  }
}
</style>
